package com.fm.cotroller;

import com.fm.common.Const;
import com.fm.common.ResultResponse;
import com.fm.pojo.User;
import com.fm.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService userService;

    //商户注册
    @RequestMapping(value = "register", method = RequestMethod.POST)
    public ResultResponse<String> register(User user){
        return userService.register(user);
    }

    //查看所有普通用户
    @RequestMapping("list")
    @ResponseBody
    public  ResultResponse userList(HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                    @RequestParam(value = "pageSize",defaultValue = "10")int pageSize){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user!=null&&!user.getRole().equals(Const.Role.ROLE_CUSTOMER)){
            return userService.userList(pageNum,pageSize);
        }
        return ResultResponse.isErrorMessage("用户未登录");
    }


}
